package cn.xjfcloud.onedriveserver.util;

import java.io.File;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.xjfcloud.onedriveserver.pojo.FileInfo;

public class FileSystemUtil {

	private static Integer initId = 0;
	
	public static  FileInfo getDirctoryAllFile(String path) {
		FileInfo rootFile = new FileInfo();
		rootFile.setFilePath(path);
		rootFile.setIsDirectory(true);
		rootFile.setId(initId);
		rootFile.setTitle("root_file");
		getChildFile(path, rootFile);
		initId ++;
		return rootFile;
	}
	
	private static void getChildFile(String path, FileInfo parentFileInfo){
		File file = new File(path);
		File[] files = file.listFiles();
		for (File file2 : files) {
			FileInfo childFileInfo= new FileInfo();
			childFileInfo.setId(initId);
			childFileInfo.setTitle(file2.getName());
			//判断是否是文件夹
			if (file2.isDirectory()) {
				childFileInfo.setFilePath(file2.getAbsolutePath());
				childFileInfo.setIsDirectory(true);
				//递归
				getChildFile(file2.getAbsolutePath(), childFileInfo);
			} else {
				childFileInfo.setFilePath(file2.getAbsolutePath());
				childFileInfo.setIsDirectory(false);
			}
			//加入list
			parentFileInfo.getChildren().add(childFileInfo);
			initId ++;
		}
	}
}
